package com.cgbl.cg.ui.collection

import android.os.Bundle
import com.alibaba.android.arouter.facade.annotation.Route
import com.cgbl.cg.BR
import com.cgbl.cg.R
import com.cgbl.cg.adapter.DiscoveryCollectionAdapter
import com.cgbl.cg.adapter.DiscoveryWorkAdapter
import com.cgbl.cg.databinding.AppActivityDiscoveryCollectionBinding
import com.cgbl.cg.view.SelectCoursePop
import com.cgbl.cg.vm.collection.DiscoveryCollectionViewModel
import com.lxj.xpopup.XPopup
import com.robot.base.base.activity.GLBaseActivity
import com.robot.base.configs.AppConstants
import com.robot.base.model.CollectDetailBean
import com.robot.base.model.WorksBean
import com.robot.base.utils.pic.ImageLoaderUtils
import com.youth.banner.adapter.BannerImageAdapter
import com.youth.banner.holder.BannerImageHolder

@Route(path = AppConstants.Router.COLLECTION.A_DISCOVERY_COLLECTION)
class DiscoveryCollectionActivity :
    GLBaseActivity<AppActivityDiscoveryCollectionBinding, DiscoveryCollectionViewModel>() {
    private lateinit var discoveryCollectionAdapter: DiscoveryCollectionAdapter
    private lateinit var discoveryWorkAdapter: DiscoveryWorkAdapter
    var id = ""
    override fun initContentView(savedInstanceState: Bundle?): Int =
        R.layout.app_activity_discovery_collection

    override fun initVariableId(): Int = BR.viewModel

    override fun initComponents() {
        initBanner()
        initAdapter()
        viewModel.id = id
        viewModel.allType()
        viewModel.page()
    }

    override fun initParam() {
        super.initParam()
        id = intent.getStringExtra("id") ?: ""
    }

    private fun initBanner() {
        binding.banner.setAdapter(object : BannerImageAdapter<Int>(
            arrayListOf(
                R.mipmap.app_bg_banner,
                R.mipmap.app_bg_banner,
                R.mipmap.app_bg_banner
            )
        ) {
            override fun onBindView(
                holder: BannerImageHolder?,
                data: Int?,
                position: Int,
                size: Int
            ) {
                ImageLoaderUtils.getGlideManager()
                    .load(data)
//                    .apply(ImageLoaderUtils.getRoundCornerGlideOption(10))
                    .into(holder?.imageView!!)
            }
        })
        binding.banner.start()
    }

    private fun initAdapter() {
        discoveryCollectionAdapter = DiscoveryCollectionAdapter()
        binding.mRvCollect.apply {
            adapter = discoveryCollectionAdapter
        }

        discoveryWorkAdapter = DiscoveryWorkAdapter()
        binding.mRvWork.apply {
            adapter = discoveryWorkAdapter
        }
//        discoveryWorkAdapter.setNewInstance(arrayListOf("", "", "", "", "", ""))
    }

    override fun initViewObservable() {
        super.initViewObservable()
        viewModel.uc.refreshEvent.observe(this) {
            it ?: return@observe
            binding.smartRefreshLayout.finishRefresh()
            binding.smartRefreshLayout.finishLoadMore()
            binding.smartRefreshLayout.setEnableLoadMore(it != 1)
        }

        binding.smartRefreshLayout.setOnRefreshListener {
            viewModel.refresh()
        }
        binding.smartRefreshLayout.setOnLoadMoreListener {
            viewModel.next()
        }

        viewModel.uc.hotCollectData.observe(this){
            discoveryCollectionAdapter.setNewInstance(it as MutableList<CollectDetailBean>?)
        }

        viewModel.uc.refreshDataEvent.observe(this) {
            if (viewModel.pageNo == 1) {
                discoveryWorkAdapter.setNewInstance(it as MutableList<WorksBean>?)
            }else{
                discoveryWorkAdapter.addData(it)
            }
        }

        viewModel.uc.selectType.observe(this) {
            val selectCoursePop =
                SelectCoursePop(this, viewModel.typeName.get() ?: "", viewModel.list)
            XPopup.Builder(this)
                .atView(binding.tvTitle)
                .asCustom(selectCoursePop)
                .show()

            selectCoursePop.setConfirmClickListener {
                selectCoursePop.dismiss()
                viewModel.typeName.set(it.name)
                viewModel.id = it.id ?: ""
                viewModel.refresh()
            }
        }
    }
}